package cn.miss;


import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.function.Consumer;

public class FileSelectDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JPanel jPanel;
    private JList allList;
    private JButton allSelect;
    private JButton allRemove;
    private JList selectList;
    private JButton singleSelect;
    private JButton singleRemove;
    private List<String> list;
    private Consumer<List<String>> listConsumer;


    public FileSelectDialog() {
        pack();
        setSize(1000, 500);
        setLocationRelativeTo(null);
        setContentPane(contentPane);
        setModal(true);
        DefaultListModel<Object> model2 = new DefaultListModel<>();
        selectList.setModel(model2);
        getRootPane().setDefaultButton(buttonOK);
        buttonOK.addActionListener(e -> onOK());
        singleSelect.addActionListener(e -> singleSelect(allList, selectList));
        singleRemove.addActionListener(e -> singleSelect(selectList, allList));
        allSelect.addActionListener(e -> allSelect(allList, selectList));
        allRemove.addActionListener(e -> allSelect(selectList, allList));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("FileSelectDialog");
        FileSelectDialog dialog = new FileSelectDialog();
        dialog.list = new ArrayList<>();
        for (int i = 0; i < dialog.allList.getModel().getSize(); i++) {
            dialog.list.add((String) dialog.allList.getModel().getElementAt(i));
        }
        frame.setContentPane(dialog.contentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    private void onOK() {
        dispose();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < selectList.getModel().getSize(); i++) {
            list.add((String) selectList.getModel().getElementAt(i));
        }
        System.out.println(list);
        listConsumer.accept(list);
    }

    private void onCancel() {
        dispose();
    }

    public void setCallable(Consumer<List<String>> listConsumer) {
        this.listConsumer = listConsumer;
    }

    //数据交换
    private void singleSelect(JList list1, JList list2) {
        DefaultListModel model1 = (DefaultListModel) list1.getModel();
        DefaultListModel model2 = (DefaultListModel) list2.getModel();
        list1.getSelectedValuesList().forEach(o -> {
            model1.removeElement(o);
            model2.addElement(o);
        });
    }

    private void allSelect(JList list1, JList list2) {
        DefaultListModel model1 = (DefaultListModel) list1.getModel();
        DefaultListModel model2 = (DefaultListModel) list2.getModel();
        Enumeration elements = model1.elements();
        while (elements.hasMoreElements()) {
            model2.addElement(elements.nextElement());
        }
        model1.removeAllElements();
    }


    public void setList(List<String> list) {
        allList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        allList.removeAll();
        if (selectList.getModel().getSize() > 0) {
            DefaultListModel model = (DefaultListModel) selectList.getModel();
            model.removeAllElements();
        }
        if (allList.getModel().getSize() > 0) {
            DefaultListModel model = (DefaultListModel) selectList.getModel();
            model.removeAllElements();
        }
        DefaultListModel model = new DefaultListModel();
        list.forEach(model::addElement);
        allList.setModel(model);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        buttonOK = new JButton();
        buttonOK.setText("完成");
        panel2.add(buttonOK, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel2.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        jPanel = new JPanel();
        jPanel.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(jPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(7, 1, new Insets(0, 0, 0, 0), -1, -1));
        jPanel.add(panel3, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        allRemove = new JButton();
        allRemove.setText("<<");
        panel3.add(allRemove, new GridConstraints(5, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        singleSelect = new JButton();
        singleSelect.setText(">");
        panel3.add(singleSelect, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        allSelect = new JButton();
        allSelect.setText(">>");
        panel3.add(allSelect, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        singleRemove = new JButton();
        singleRemove.setText("<");
        panel3.add(singleRemove, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer2 = new Spacer();
        panel3.add(spacer2, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final Spacer spacer3 = new Spacer();
        panel3.add(spacer3, new GridConstraints(6, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final Spacer spacer4 = new Spacer();
        panel3.add(spacer4, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        jPanel.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(400, 200), new Dimension(400, 600), new Dimension(400, 800), 0, false));
        allList = new JList();
        final DefaultListModel defaultListModel1 = new DefaultListModel();
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        defaultListModel1.addElement("s");
        allList.setModel(defaultListModel1);
        scrollPane1.setViewportView(allList);
        final JScrollPane scrollPane2 = new JScrollPane();
        jPanel.add(scrollPane2, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(400, 200), new Dimension(400, 600), new Dimension(400, 600), 0, false));
        selectList = new JList();
        scrollPane2.setViewportView(selectList);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
